package com.wt.admin.service.ai.impl.agents;

import com.wt.admin.domain.dto.ai.AgentsInfoDTO;
import com.wt.admin.domain.entity.ai.MCPEntity;
import com.wt.admin.domain.entity.ai.ModelConfigEntity;
import io.micrometer.observation.ObservationRegistry;
import org.springframework.ai.model.tool.ToolCallingManager;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("OpenAI")
public class OpenAIBuilder extends AbstractAgentsBuilderService {

    @Override
    public AgentsManager.ChatClientMapper builder(AgentsInfoDTO agents, ModelConfigEntity model, List<MCPEntity> mcpEntities) {
        // 查询模型信息
        var openAiApi = OpenAiApi.builder()
                .baseUrl(model.getBaseUrl())
                .apiKey(model.getAppKey())
                .build();
        OpenAiChatOptions options = OpenAiChatOptions.builder().model(model.getModel()).build();
        OpenAiChatModel openAi = new OpenAiChatModel(
                openAiApi,
                options,
                ToolCallingManager.builder().build(),
                RetryTemplate.defaultInstance(),
                ObservationRegistry.NOOP
        );
        // 在构建完模型后，对模型做其他初始化
        return build(openAi, agents, mcpEntities, options);
    }

}
